home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / Trench-c / Trench.c < prev    next >
Encoding:
Text File  |  1994-12-04  |  5.7 KB  |  191 lines  |  [TEXT/KAHL]

  1. //• ----------------------------------------------------------------------•//
  2. //• Updated to run in the Think C™ environment by Kenneth A. Long on
  3. //• 12 March 1992.
  4. //• Add MacTraps and ANSI.
  5. //• ----------------------------------------------------------------------•//
  6.  
  7. //• -------------------- Source to "Trench Simulator" ------------------- •//
  8. //• Written 12:00 pm  Jul 16, 1985 by 
  9. //• sdh@joevax.UUCP (Steve Hawley)
  10. //• Posted in uiucdcs:net.sources.mac.
  11. //• This is the source to the trench simulator. It was written in MegaMax C.
  12. //• Note the tabs should be set to 4 spaces.
  13.  
  14. //• Program to simulate the trench from Star Wars.
  15. //• Written 12 July 85 by Steve Hawley in MegaMax C.
  16.  
  17. //#include <qd.h>        //• QuickDraw header file.
  18. //#include <qd.h>        //• Quickdraw variables.
  19. //#include <Event.h>      //• Event manager header.
  20. //#include <win.h>        //• Window manager header.
  21.  
  22. WindowRecord wRecord;     //• Record for window.
  23. WindowPtr myWindow;        //• Pointer to Record.
  24. Rect screenRect;        //• Rectangle representing window size.
  25.  
  26. int wlines = 0;            //• phase of depth lines.
  27.  
  28. //• ----------------------------------------------------------------------•//
  29.  
  30. //• ----------------------------------------------------------------------•//
  31. //• draws lines to give illusion of depth.
  32. //• ----------------------------------------------------------------------•//
  33.  
  34. Draw_Lines (int offx, int offy, int start) 
  35. {
  36.     int x1 = -200, y1 = -100;
  37.     int z, index;
  38.  
  39.     //• Start is the phase (from 0 to 3)>
  40.     //• The lines are projected by the formulae:
  41.  
  42.     //•     x' = x / z; 
  43.     //•     y' = y / z;
  44.  
  45.     //• offx and offy are offsets for viewPoint changes.
  46.     
  47.     z = 10000;        //• KAL added this to keep from dividing by zero.
  48.                     //• in the built app.  Looks and acts the same.
  49.                     
  50.     for (index = 50 - start; index > 0; index -= 4)
  51.     {
  52.         MoveTo (( x1 - offx) / z, 
  53.                 ( y1 - offy) / z);
  54.         LineTo (( x1 - offx) / z, 
  55.                 (-y1 - offy) / z);
  56.                 
  57.         MoveTo ((-x1 - offx) / z, 
  58.                 ( y1 - offy) / z);
  59.         LineTo ((-x1 - offx) / z, 
  60.                 (-y1 - offy) / z);
  61.     }
  62. }
  63.  
  64. //• ----------------------------------------------------------------------•//
  65. //• Draws the frame of the trench.
  66. //• offx and offy again represent the viewPoint offsets, and it is.
  67. //• Projected using the same formulae as before.
  68. //• ----------------------------------------------------------------------•//
  69.  
  70. SetUp (short offx, short offy) 
  71. {
  72.  
  73.     int x1 = -200, x2 = -100, y1 = -100;
  74.     
  75.     
  76.     //• The 50 sets the far end of the lines.  Over 50 makes no noticable
  77.     //• changes.  Less than 50 brings the ends closer.  Try 4.
  78.     
  79.     MoveTo     (  x1 - offx,              
  80.                y1 - offy);
  81.                
  82.     LineTo ((  x1 - offx)    / 50,     
  83.             (  y1 - offy)    / 50);
  84.             
  85.     LineTo ((  x1 - offx)    / 50,     
  86.             (- y1 - offy)   / 50);
  87.             
  88.     LineTo     (  x1 - offx, -            
  89.                y1 - offy);
  90.     
  91.     MoveTo (   x2 - offx,    - 
  92.                y1 - offy);
  93.                
  94.     LineTo ((  x2 - offx)    / 50,
  95.             (- y1 - offy)   / 50);
  96.     
  97.     MoveTo     (- x1 - offx,              
  98.                y1 - offy);
  99.                
  100.     LineTo ((- x1 - offx)    / 50,     
  101.             (  y1 - offy)   / 50);
  102.             
  103.     LineTo ((- x1 - offx)    / 50,     
  104.             (- y1 - offy)   / 50);
  105.     LineTo  (- x1 - offx,    -            
  106.                y1 - offy);
  107.     
  108.     MoveTo     (- x2 - offx, -
  109.                y1 - offy);
  110.                
  111.     LineTo ((- x2 - offx)    / 50,     
  112.             (- y1 - offy)   / 50);
  113. }
  114.  
  115. //• ----------------------------------------------------------------------•//
  116. //• The objects are animated by using exclusive-or drawing. 
  117.  
  118. //• This way, the same routine to draw can be used to erase, and the new 
  119. //• image can be drawn before the old image is erased to help eliminate 
  120. //• flicker.  
  121.  
  122. //• Thus the the program needs two copies of the offset parameters, one
  123. //• for the new and one for the old.
  124. //• ----------------------------------------------------------------------•//
  125.  
  126. main ()
  127. {
  128.     int offxo = 0, offxn = 0, offyo = 0, offyn = 0;
  129.     Point mouse;
  130.     
  131.     InitGraf (&thePort);            //• Set up quickdraw.
  132.     FlushEvents (everyEvent, 0);     //• Kill previous Events.
  133.     InitWindows ();                    //• Initialize window manager.
  134.     
  135.     InitCursor ();                    //• Ken Long added to get rid of
  136.                                     //• default text edit cursor.
  137.     //• Set window size.
  138.     SetRect (&screenRect, 4, 40, 508, 338); 
  139.     
  140.     //• Draw one from scratch.
  141.     myWindow = NewWindow (&wRecord, &screenRect, "\pTrench",1,0, (WindowPtr)-1L, 1, 0L);
  142.     
  143.     //• Set window parameters.
  144.     SetPort (myWindow);             //• Make the window the current grafport.
  145.     ShowWindow (myWindow);             //• Display window.
  146.  
  147.     //• Set the origin so the center of the screen in (0,0).
  148.     SetOrigin (-252, -149);
  149.     PenMode (patXor);                 //• Set exclusive-or drawing.
  150.     
  151.     SetUp (offxn, offyn);             //• Draw Initial SetUp.
  152.     
  153.     Draw_Lines (offxn, offyn, wlines); //• Draw Initial depth lines.
  154.     
  155.     while (!Button ())                 //• Repeat until Button is clicked.
  156.     {
  157.         offxo = offxn;               //• Put new offsets in old variables.
  158.         offyo = offyn;
  159.         GetMouse (&mouse);             //• get the mouses coordinates.
  160.         
  161.         //• We divide the mouse location by two so we don't go
  162.         //• trough the walls or floor of the trench.
  163.         if (mouse.h /2 < offxn)      //• If the horizontal has changed,
  164.             offxn =  mouse.h /2;    //• store it in the new offset.
  165.         else 
  166.             if (mouse.h /2 > offxn)
  167.                 offxn = mouse.h / 2;
  168.                 
  169.         if (mouse.v /2 < offyn)        //• If the vertical has changed,
  170.             offyn = mouse.v / 2;    //• store it in the new offset.
  171.         else 
  172.             if (mouse.v /2 > offyn)
  173.                 offyn = mouse.v / 2;
  174.                 
  175.         //• If the old offset ain't the same as the new one...
  176.         if ((offxo != offxn) || (offyo != offyn))     
  177.         {                                                                                //• differs from the new, update.
  178.             SetUp (offxn, offyn); //• draw the new setup...
  179.             SetUp (offxo, offyo); //• and erase the old one.
  180.         }
  181.         Draw_Lines (offxo, offyo, wlines); //• Erase the vertical lines.
  182.         wlines++; //• Increment wlines.
  183.         if (wlines > 3) 
  184.             wlines = 0; //• Reset wlines if too big.
  185.             
  186.             //• Draw new set of lines.
  187.             Draw_Lines (offxn, offyn, wlines); 
  188.     }
  189. }
  190.  
  191.